//
//  HomeViewController.m
//  Codingke2016
//
//  Created by xujiangtao on 16/5/30.
//  Copyright © 2016年 xujiangtao. All rights reserved.
//

#import "HomeViewController.h"
#import "HomeCollectionViewCell.h"
#import "HomeHeadView.h"

@interface HomeViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
{

    UICollectionView *_mainCollection;
    UIView *_rollView;// 轮播图
    float _itemWidth;
}
@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self makeCollectionView];
    
    
}
-(void)makeCollectionView{
    
    _itemWidth = (SCREEN_WIDTH - 10 * 3) / 2;

    // 图片的宽高比为 33/20 ，加上下面的文字部分 70
    UICollectionViewFlowLayout* flowLayout = [[UICollectionViewFlowLayout alloc] init];
    [flowLayout setItemSize:CGSizeMake(_itemWidth, (_itemWidth * 20) / 33+70)];
    [flowLayout setScrollDirection:UICollectionViewScrollDirectionVertical];
    flowLayout.headerReferenceSize = CGSizeMake(SCREEN_WIDTH, 50.0f);  //设置head大小


    _mainCollection = [[UICollectionView alloc] initWithFrame:self.view.bounds collectionViewLayout:flowLayout];
    // mainCollectionView.alwaysBounceHorizontal=YES;
    _mainCollection.alwaysBounceVertical=YES;
    [_mainCollection setDataSource:self];
    [_mainCollection setDelegate:self];
    [_mainCollection setBackgroundColor:[UIColor whiteColor]];
    [self.view addSubview:_mainCollection];

     [_mainCollection registerClass:[HomeCollectionViewCell class] forCellWithReuseIdentifier:@"homeCellIdentifier"];
    
#pragma mark -- 注册头部视图
    [_mainCollection registerClass:[HomeHeadView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView"];
    
    // 轮播图宽高比 ：360/145
    _rollView=[[UIView alloc] initWithFrame:CGRectMake(0,0,SCREEN_WIDTH,(SCREEN_WIDTH*145)/360)];
    _rollView.backgroundColor=[UIColor colorWithRed:244/255.0 green:244/255.0 blue:244/255.0 alpha:1];
    _rollView.backgroundColor =[UIColor blueColor];
    [_mainCollection addSubview:_rollView];
    
   
}
#pragma mark - UICollectionView Delegate
-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{

    return 3;
}
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
   
    NSInteger result;
    
    if(section==0){
    
        result = 4;
    }else if (section ==1){
        result = 3;
    }else{
    
        result = 5;
    }
    
    return  result;
}
- (UICollectionViewCell*)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    //2 从复用池中找cell(1:cell的标示符 2:indexPath决定系统用不用再给你创建cell, 不用创建的话, 就直接使用之前的cell)
    
    static NSString* cellIdentifier = @"homeCellIdentifier";
    HomeCollectionViewCell* homeCell = [collectionView dequeueReusableCellWithReuseIdentifier:cellIdentifier forIndexPath:indexPath];
 //   MainModelObject *model=[mainArr objectAtIndex:indexPath.row];
 //   [HomeCollectionViewCell loadInterfacWithModel:model];
    
    return homeCell;
}
// 设置collection Section的header
-(UICollectionReusableView*)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{

    UICollectionReusableView *reusableView = nil;

    if (kind == UICollectionElementKindSectionHeader) {
        //定制头部视图的内容
        HomeHeadView *headerV = (HomeHeadView *)[collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"HeaderView" forIndexPath:indexPath];
        if(indexPath.section ==0)
        {
            headerV.titleLab.text = @"头部视图YES";
        }else if (indexPath.section ==1){
            headerV.titleLab.text = @"头部视图NO";
        }else{
           headerV.titleLab.text = @"头部视图OK";
        }
        reusableView = headerV;
    }
    
    return reusableView;
    
}
#pragma mark- UICollectionViewDelegateFlowLayout
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout insetForSectionAtIndex:(NSInteger)section{
    
    if(section == 0){
        
       return UIEdgeInsetsMake(_rollView.frame.size.height, 10, 0, 10);
        
    }else{
        
        return UIEdgeInsetsMake(0, 10, 0, 10);
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
